package online.westonline.com.westonline.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;
import android.provider.ContactsContract;
import android.telephony.PhoneNumberUtils;

/**
 * 对联系人操作的工具类
 * Created by xiatao on 2018/6/25.
 */

public class ContactsUtils {
    private ContactsUtils()
    {

    }

    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    public static void diallPhone(String phoneNum,Context oneContactActivity) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        oneContactActivity.startActivity(intent);
    }

    /**
     * 拨打电话（直接拨打电话）
     *Call_Phone权限
     * @param phoneNum 电话号码
     * @param oneContactActivity
     */
    @SuppressLint("MissingPermission")
    public static void callPhone(String phoneNum, Context oneContactActivity) {
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        oneContactActivity.startActivity(intent);
    }

    /**
     * 调起系统发短信功能
     * @param phoneNumber
     * @param message
     */
    public static void doSendSMSTo(String phoneNumber,String message,Context context){
        if(PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber)){
            Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"+phoneNumber));
            intent.putExtra("sms_body", message);
            context.startActivity(intent);
        }
    }

    /**
     * 发送邮件
     * @param email 电话
     * @param oneContactActivity
     */
    public static void doSendEmail(String email, Context oneContactActivity)
    {
        Uri uri = Uri.parse("mailto:"+email);
        Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
        intent.putExtra(Intent.EXTRA_CC, email); // 抄送人
        intent.putExtra(Intent.EXTRA_SUBJECT, "这是邮件的主题部分"); // 主题
        intent.putExtra(Intent.EXTRA_TEXT, "这是邮件的正文部分"); // 正文
        oneContactActivity.startActivity(Intent.createChooser(intent, "请选择邮件类应用"));
    }

    /**
     * 添加联系人
     * @param
     */
    public static void savePeople(String phone,String name,Context context)
    {
        Intent intent = new Intent(Intent.ACTION_INSERT);
        intent.setType("vnd.android.cursor.dir/person");
        intent.setType("vnd.android.cursor.dir/contact");
        intent.setType("vnd.android.cursor.dir/raw_contact");
// 添加姓名
        intent.putExtra(ContactsContract.Intents.Insert.NAME,name);
// 添加手机
        intent.putExtra(ContactsContract.Intents.Insert.PHONE_TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
        intent.putExtra(ContactsContract.Intents.Insert.PHONE, phone);
        context.startActivity(intent);
    }

    /**
     * 圆形bitmap
     *
     * @param bitmap
     * @return
     */
    public static Bitmap getOvalBitmap(Bitmap bitmap){

        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                .getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

        final RectF rectF = new RectF(rect);

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);

        canvas.drawOval(rectF, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }
}
